package com.shj.em.aa.service;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.shj.em.aa.entity.UserRole;
import com.shj.em.aa.mapper.RoleMapper;
import com.shj.em.aa.mapper.UserRoleMapper;
import com.shj.em.aa.service.UserRoleService;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.shj.em.util.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;


/**
 * <p>
 * 用户与角色对应关系 服务实现类
 * </p>
 *
 * @author Shen Huang Jian
 * @since 2018-06-25
 */
@Service
public class UserRoleServiceImpl extends ServiceImpl<UserRoleMapper, UserRole> implements UserRoleService {

    @Autowired
    UserRoleMapper userRoleMapper;

    public void updateRoleUserMapping(Long roleId, String userIds){
        Wrapper<UserRole> wrapper = new EntityWrapper<>();
        wrapper.eq("del_flag", 0).eq("role_id", roleId);

        if(StringUtils.isBlank(userIds)){
            userRoleMapper.delete(wrapper);
            return;
        }
        List<UserRole> list = userRoleMapper.selectList(wrapper);
        String[] userIdArr = userIds.split(";");

        // 删掉不在 userIds 中的对应关系
        List<Long> needToDelete = new ArrayList<>();
        for(UserRole ur : list){
            boolean flag = true;
            for(String userId : userIdArr){
                if(ur.getUserId().equals(userId)){
                    flag = false;
                    break;
                }
            }
            if(flag){
                needToDelete.add(ur.getId());
            }
        }

        if(needToDelete.size() > 0){
            userRoleMapper.deleteBatchIds(needToDelete);
        }

        // 添加新增的
        List<Long> needToAdd = new ArrayList<>();
        for(String userId : userIdArr){
            boolean flag = true;
            for(UserRole ur : list){
                if(ur.getUserId().equals(userId)){
                    flag = false;
                    break;
                }
            }
            if(flag){
                needToAdd.add(Long.parseLong(userId));
            }
        }

        for(Long userId : needToAdd){
            UserRole userRole = new UserRole();
            userRole.setRoleId(roleId);
            userRole.setUserId(userId);
            userRoleMapper.insert(userRole);
        }
    }

    public void updateUserRoleMapping(Long userId, String roleIds){
        Wrapper<UserRole> wrapper = new EntityWrapper<>();
        wrapper.eq("del_flag", 0).eq("user_id", userId);

        if(StringUtils.isBlank(roleIds)){
            userRoleMapper.delete(wrapper);
            return;
        }
        List<UserRole> list = userRoleMapper.selectList(wrapper);
        String[] roleIdArr = roleIds.split(";");

        // 删掉不在 userIds 中的对应关系
        List<Long> needToDelete = new ArrayList<>();
        for(UserRole ur : list){
            boolean flag = true;
            for(String roleId : roleIdArr){
                if(ur.getRoleId().equals(roleId)){
                    flag = false;
                    break;
                }
            }
            if(flag){
                needToDelete.add(ur.getId());
            }
        }

        if(needToDelete.size() > 0){
            userRoleMapper.deleteBatchIds(needToDelete);
        }

        // 添加新增的
        List<Long> needToAdd = new ArrayList<>();
        for(String roleId : roleIdArr){
            boolean flag = true;
            for(UserRole ur : list){
                if(ur.getRoleId().equals(roleId)){
                    flag = false;
                    break;
                }
            }
            if(flag){
                needToAdd.add(Long.parseLong(roleId));
            }
        }

        for(Long roleId : needToAdd){
            UserRole userRole = new UserRole();
            userRole.setUserId(userId);
            userRole.setRoleId(roleId);
            userRoleMapper.insert(userRole);
        }
    }
}
